/**
 * Created by HY on 2015/1/17 0017.
 */

var TYPE_GET = "GET", TYPE_POST = "POST", DATA_TYPE_HTML = "html", DATA_TYPE_JSON = "json", DATA_TYPE_STRING = "String";

function getVal(id) {
	return $("#" + id).val();
}

function getHtml(id) {
	return $("#" + id).html();
}

/**
 * 过滤所有html标签
 *
 * @param str
 * @returns {XML|string|void}
 */
function filterHtml(str) {
	return str.replace(/<\/?[^>]*>/g, "");
}

/**
 * 过滤&nbsp;以及空格空字符串等
 *
 * @param str
 * @returns {*}
 */
function filterSpace(str) {
	str = str.replace(/&nbsp;/ig, "");
	return $.trim(str);//去除字符串空格,正则似乎str.replace(/\s|\xA0/g,"")才行,没测试过
}

function setVal(id, value) {
	$("#" + id).val(value);
}

function formSubmit(id) {
	$("#" + id).submit();
}

function notEmpty(val) {
	if (val == null || val.length == 0) {
		return false;
	}
	return true;
}

function getId(obj) {
	return $(obj).attr("id");
}

function defaultResponse(data) {
	if (!data.success) {
		alert(data.msg);
	} else {
		alert("操作成功");
	}
}


/*************************
 *
 * ajax form提交封装
 *
 **************************/

function ajaxSubmitJson(id, json_params, response) {
	ajaxSubmit(id, json_params, DATA_TYPE_JSON, response);
}

function ajaxObjectSubmitJson(obj, json_params, response) {
	$(obj).ajaxSubmit({
		data: json_params,
		dataType: DATA_TYPE_JSON,
		success: response
	})
}

function ajaxSubmit(id, json_params, data_type, response) {
	$("#" + id).ajaxSubmit({
		data: json_params,
		dataType: data_type,
		success: response
	})
}


/*************************
 *
 * ajax请求封装
 *
 **************************/

function ajaxRequestGetHtml(href, json_params, response) {
	ajaxRequest(href, json_params, response, TYPE_GET, DATA_TYPE_HTML);
}

function ajaxRequestGetJson(href, json_params, response) {
	ajaxRequest(href, json_params, response, TYPE_GET, DATA_TYPE_JSON);
}

function ajaxRequestGetString(href, json_params, response) {
	ajaxRequest(href, json_params, response, TYPE_GET, DATA_TYPE_STRING);
}

function ajaxRequest(href, json_params, response, type, data_type) {
	$.ajax({
		url: href,
		data: json_params,
		type: type,
		dataType: data_type,
		success: response
	});
}